Back to Portfolio
Academic Private

Catscript Compiler

A complete, functional compiler built in Java that translates a custom high-level programming language into executable Java Virtual Machine (JVM) bytecode.

Java Lexical Analysis Parsing JVM Bytecode
Catscript.ebnf
catscript_program = { program_statement };

program_statement = statement | function_declaration;

statement = for_statement |
            if_statement |
            print_statement |
            variable_statement |
            assignment_statement |
            function_call_statement;

for_statement = 'for', '(', IDENTIFIER, 'in', expression, ')',
                '{', { statement }, '}';

if_statement = 'if', '(', expression, ')', '{',
               { statement },
               '}' [ 'else', ( if_statement | '{', { statement }, '}' ) ];

The Challenge

Building a compiler requires a deep understanding of computer architecture, data structures, and algorithmic logic. The goal was to take "Catscript"—a custom, statically typed language—and map its syntax and semantics perfectly to JVM bytecode so it could run on any machine with Java installed, without relying on external compiler-generation tools like ANTLR.

Architecture & Implementation

Tokenizer (Lexer)

Engineered a custom scanner to read raw source code character by character, utilizing regular expressions and state machines to group them into meaningful tokens (keywords, identifiers, literals) while stripping whitespace and comments.

Parser (AST)

Implemented a recursive descent parser to analyze the stream of tokens against the language's formal grammar, successfully constructing an Abstract Syntax Tree (AST) to represent the structural logic of the program.

Semantic Analysis

Traversed the AST to enforce type safety, verify variable scope declarations, and catch semantic errors before code generation, ensuring the resulting program would not crash at runtime.

Code Generation

Mapped the validated AST to Java Virtual Machine instructions (Opcodes), managing the operand stack and local variable arrays to output a fully functional `.class` file executable by the JVM.

Outcomes & Takeaways

Successfully passing hundreds of rigorous unit tests, this project solidified my understanding of low-level application logic. It drastically improved my ability to write highly optimized, memory-safe Java code and gave me a profound appreciation for what happens under the hood when a script is executed.